<?php
class Administrator_FileController extends QSource_System_Controller_Action{
	public function init(){
		$this->loadTemplate('administrator');
	}
	public function indexAction(){ 
	    $model = new Model_File_File();
	    $data = $model->getAll();
	    Zend_View_Helper_PaginationControl::setDefaultViewPartial(APPLICATION_PATH.'\modules\administrator\views\pagination.phtml');
	    $pa = Zend_Paginator::factory($data);
	    $pa->setItemCountPerPage(10);
	    $current = $this->_request->getParam('page');
	    $this->view->page = $current;
	    $pa->setCurrentPageNumber($current);
	    $this->view->data = $pa;
	}
	public function addAction(){
		$model = new Model_File_File();
		if ($this->_request->isPost()) {
		    $error = array();
			$data = $this->_request->getParams();
			// Kiểm tra và upload hình ảnh
		    	$name = '';
		    	//Xử lý hình ảnh
		    	$imageUpload = new Zend_File_Transfer_Adapter_Http();
		    	$imageUpload->setDestination(
		    			realpath(APPLICATION_PATH . '/../public/images/files/'.$data['category']));
		    	//Set Image Validator
		    	$imageUpload->addValidator('Size', false,
		    			array('min' => 2000, 'max' => 2097152));
		    	$imageUpload->addValidator('Extension', false,
		    			array('jpg', 'png', 'gif', 'jpeg'));
		    	$imageInfo = $imageUpload->getFileInfo();
		    	foreach ($imageInfo as $file => $fileinfo) {
		    		if (($imageUpload->isUploaded($fileinfo['name'])) &&
		    				($imageUpload->isValid($fileinfo['name'])))
		    			if (! $imageUpload->receive($fileinfo['name'])) {
		    			$error['image'] = $imageUpload->getMessages();
		    		} else {
		    			$name = $fileinfo['name'];
		    		}
		    	}
		    if($name == '')
		        $error['name'] = 'Bạn chưa upload file. Vui lòng chọn file.';
			if (count($error) > 0) {
				$this->view->error = $error;
				$this->view->data = $data;
			} else {
				$date_cre= date('Y-m-d H:i:s',time());
	    		$user_cre = $this->user->id;
	    		$item = array('name'=>$name,'link'=>$data['link'],'alt'=>$data['alt'],'rel'=>$data['rel'],'title'=>$data['title'],'type'=>0,'date_cre'=>$date_cre,'user_cre'=>$user_cre,'category'=>$data['category'],'status'=>$data['status']);
	    		$model->insert($item);
	    		$this->view->success = 'Đã thêm file mới thành công';
			}
			
		}
	}
	public function editAction(){
		$model = new Model_File_File();
		$id = $this->_request->getParam('id','');
		if ($this->_request->isPost()) {
			$error = array();
			$data = $this->_request->getParams();
			if (count($error) > 0) {
				$this->view->error = $error;
				$this->view->data = $data;
			} else {
				$item = array('link'=>$data['link'],'alt'=>$data['alt'],'rel'=>$data['rel'],'title'=>$data['title'],'status'=>$data['status']);
				$model->update($item,'id = '.$id);
				$this->_redirect('/administrator/file');
			}
				
		}
		else
		{
		   $data = $model->getItemId($id);
		   $this->view->data = $data;
		}
	}
	public function searchAction(){
		$keyword = $this->_request->getParam('keyword','');
		if($keyword == '')
			$this->_redirect('/administrator/file');
		$model = new Model_File_File();
		$data = $model->getAllStartWith($keyword);
		Zend_View_Helper_PaginationControl::setDefaultViewPartial(
		APPLICATION_PATH . '\modules\administrator\views\pagination.phtml');
		$pa = Zend_Paginator::factory($data);
		$pa->setItemCountPerPage(10);
		$current = $this->_request->getParam('page');
		$this->view->page = $current;
		$pa->setCurrentPageNumber($current);
		$this->view->data = $pa;
		$this->renderScript('file/index.phtml');
	}
	
	//Data name Json - Data autocomplete for jqueryui
	public function datanamejsonAction(){
		$model = new Model_File_File();
		$term = $this->_request->getParam('term','');
		if($term=='')
			$data = $model->getAll();
		else
			$data = $model->getAllStartWith($term);
		$this->view->data = $data;
		$this->_helper->layout->disableLayout();
	}
	
	public function removeAction ()
	{
		Zend_Layout::resetMvcInstance();
		$this->_helper->viewRenderer->setNoRender();
		$id = $this->_request->getParam('id', '');
		if ($id != '') {
		    $model = new Model_File_File();
		    $item = $model->getItemId($id);
		    if ($item['name'] != '')
		    	unlink(realpath(FILE_PATH . '/'.$item['category'].'/'. $item['name']));
		    $model->delete('id = '.$id);
		}
	}
	
}